import *  as util from '@/amtf/utils'
const viewModules = import.meta.glob('@/view/**/*.vue');
const pluginModules = import.meta.glob('@/plugin/**/*.vue');
const aa插件模块 = import.meta.glob('@/aa/**/*.vue');
const layouts = import.meta.glob('@/amtf/layouts/**/*.vue');
const pages = import.meta.glob('@/amtf/pages/**/*.vue');
// const viewModules = import.meta.glob('@/view/**/*.vue', {
//   ignore: ['@/view/**/components/**/*.vue']
// });
// const pluginModules = import.meta.glob('@/plugin/**/*.vue', {
//   ignore: ['@/plugin/**/components/**/*.vue']
// });
// const aa插件模块 = import.meta.glob('@/aa/**/*.vue', {
//   ignore: ['@/aa/**/components/**/*.vue']
// });
// const layouts = import.meta.glob('@/amtf/layouts/**/*.vue', {
//   ignore: ['@/amtf/layouts/**/components/**/*.vue']
// });
// const pages = import.meta.glob('@/amtf/pages/**/*.vue', {
//   ignore: ['@/amtf/pages/**/components/**/*.vue']
// });

// 静态导入报错……
// import 父路由 from '@/amtf/layouts/父路由.vue'

const 组件模块集 = {
  ...viewModules,
  ...pluginModules,
  ...aa插件模块,
  ...pages,
  ...layouts
};

let 父路由 = () => import('@/amtf/layouts/父路由.vue')

// console.log(`组件模块集 👉`,组件模块集)
// console.log(`aa插件模块 👉`,aa插件模块)
export const asyncRouterHandle = (asyncRouter) => {
  // console.log(`asyncRouter 👉`,asyncRouter)
  // console.log(`viewModules 👉`,viewModules)
  // let page_layout = import.meta.glob('@/amtf/layouts/page_layout.vue')
  // let 父路由 = () => import('@/amtf/layouts/父路由.vue')
  asyncRouter.forEach(item => {
    if (item.component && typeof item.component === 'string') {
      // console.log(`item.name 👉`, item.name)
      // console.log(`item.path 👉`, item.path)
      // console.log(`item.component 👉`, item.component)
      let 组件全名 = "/src/" + item.component
      // console.log(`组件全名 👉`,组件全名)
      item.meta.path = 组件全名
      let 匹配组件 = 组件模块集[组件全名]
      // console.log(`匹配组件 👉`,匹配组件)
      if (item.component == 'view/superAdmin/index.vue') {
        // console.log(`view/superAdmin/index.vue item.name 👉`, item.name)
        // console.log(`item.path 👉`, item.path)
        // console.log(`item.component 👉`, item.component)
        匹配组件 = 父路由
      }
      if (item.component == 'view/routerHolder.vue') {
        匹配组件 = 父路由
      }

      if (!匹配组件) {
        let sps = item.component.split('/')
        // console.log(`sps 👉`,sps)
        let 组件首名 = sps[0]
        if (组件首名 === 'view') {
          // console.log(`item.name 👉`, item.name)
          // console.log(`item.path 👉`, item.path)
          // console.log(`item.component 👉`, item.component)
          匹配组件 = 组件模块集[组件全名]
        }
        else if (组件首名 === 'plugin') {
          item.component = dynamicImport(pluginModules, item.component)
        }
        else if (组件首名 === '找aa') {
          // console.log(`找aa item 👉`, item)
          const 文件名 = item.name

          //使得 defineProps也能接收到router的query参数👇
          // item.props = (route) => ({ ...route.query })
          // console.log(`文件名 👉`, 文件名)
          匹配组件 = 组件模块集[`/src/aa/${文件名}.vue`]
        }
        else {
          // console.log(`item.name 👉`, item.name)
          // console.log(`item.path 👉`, item.path)
          // console.log(`item.component 👉`, item.component)
        }
      }
      // 最终赋值👇
      if (匹配组件) {
        item.component = 匹配组件
      } else {
        // console.log(`硬是没匹配到组件 item 👉`,item)
      }
    }
    if (item.children) {
      // console.log(`item.children 👉`,item.children)
      asyncRouterHandle(item.children)
    }
  })
}

function dynamicImport(
  dynamicViewsModules,
  component
) {
  const keys = Object.keys(dynamicViewsModules)
  const matchKeys = keys.filter((key) => {
    const k = key.replace('../', '')
    return k === component
  })
  const matchKey = matchKeys[0]

  return dynamicViewsModules[matchKey]
}
